Dear ImGui结合CMake实现基于GLFW和OpenGL3的入门级hello world代码 您所在的位置:网站首页 imgui 中文 问号 Dear ImGui结合CMake实现基于GLFW和OpenGL3的入门级hello world代码

Dear ImGui结合CMake实现基于GLFW和OpenGL3的入门级hello world代码

2024-07-16 23:08| 来源: 网络整理| 查看: 265

Dear ImGui结合CMake实现基于GLFW和OpenGL3的入门级hello world代码

如需转载请标明出处:https://blog.csdn.net/itas109 技术交流:129518033

环境:

OS: windows 10 / Ubuntu 22.04 imgui: 1.89.5 glw: 3.3.8 前言

Dear ImGui 是一个 用于C ++的无膨胀图形用户界面库。它输出优化的顶点缓冲区,您可以随时在启用3D管线的应用程序中进行渲染。 它快速,可移植,与渲染器无关并且是独立的(无外部依赖性)。

Dear Imgui是一个非常轻量级的库,没有额外的外部依赖,支持跨平台,很适合用于游戏引擎。

官方网站:https://github.com/ocornut/imgui

官方维护的后端:

渲染器:DirectX9,DirectX10,DirectX11,DirectX12,OpenGL,OpenGL3/ES/ES2,Vulkan,Metal 平台:GLFW,SDL2,Win32,Glut,OSX 框架:Emscripten,Allegro5,Marmalade 1. 下载ImGui代码及其依赖

这里我们使用GLFW3和OpenGL3进行渲染。

ImGui 1.89.5

https://github.com/ocornut/imgui/archive/refs/tags/v1.89.5.tar.gz

glw 3.3.8

https://github.com/glfw/glfw/archive/refs/tags/3.3.8.tar.gz

2. hello world代码

目录结构

imgui-1.89.5 $ tree . |-- CMakeLists.txt # my |-- glfw-3.3.8.tar.gz # my |-- main.cpp # my ... |-- imconfig.h |-- imgui.cpp |-- imgui.h |-- imgui.ini |-- imgui_demo.cpp |-- imgui_draw.cpp |-- imgui_internal.h |-- imgui_tables.cpp |-- imgui_widgets.cpp |-- imstb_rectpack.h |-- imstb_textedit.h |-- imstb_truetype.h ...

main.cpp

// Dear ImGui: standalone example application for GLFW + OpenGL3 #include "imgui.h" #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" #include // Will drag system OpenGL headers // Main code int main(int, char**) { if (!glfwInit()) return 1; // GL 3.0 + GLSL 130 const char* glsl_version = "#version 130"; // Create window with graphics context GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3", nullptr, nullptr); if (window == nullptr) return 1; glfwMakeContextCurrent(window); glfwSwapInterval(1); // Enable vsync // Setup Dear ImGui context ImGui::CreateContext(); // Setup Platform/Renderer backends ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL3_Init(glsl_version); // Main loop while (!glfwWindowShouldClose(window)) { // Poll and handle events (inputs, window resize, etc.) glfwPollEvents(); // Start the Dear ImGui frame ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); // ************ GUI ************** ImGui::Begin("hello world"); ImGui::Text("hello world"); ImGui::End(); // ************ GUI ************** // Rendering ImGui::Render(); int display_w, display_h; glfwGetFramebufferSize(window, &display_w, &display_h); glViewport(0, 0, display_w, display_h); static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); glClear(GL_COLOR_BUFFER_BIT); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(window); } // Cleanup ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); glfwDestroyWindow(window); glfwTerminate(); return 0; }

CMakeLists.txt

cmake_minimum_required(VERSION 3.11.0) project(main) include_directories(.) include_directories(backends) set(IMGUI_FILES imgui.cpp imgui_draw.cpp imgui_tables.cpp imgui_widgets.cpp backends/imgui_impl_glfw.cpp # platform backends/imgui_impl_opengl3.cpp # render ) include(FetchContent) # cmake 3.11 above FetchContent_Declare(glfw URL ${CMAKE_CURRENT_SOURCE_DIR}/glfw-3.3.8.tar.gz) FetchContent_GetProperties(glfw) if(NOT glfw_POPULATED) # Fetch the content using previously declared details FetchContent_Populate(glfw) # Set custom variables set(BUILD_SHARED_LIBS OFF) set(GLFW_BUILD_EXAMPLES OFF) set(GLFW_BUILD_TESTS OFF) set(GLFW_BUILD_DOCS OFF) set(GLFW_INSTALL OFF) set(GLFW_VULKAN_STATIC OFF) # Bring the populated content into the build add_subdirectory(${glfw_SOURCE_DIR} ${glfw_BINARY_DIR}) endif() add_executable(${PROJECT_NAME} main.cpp ${IMGUI_FILES}) if(WIN32) target_link_libraries(${PROJECT_NAME} opengl32 glfw) else() target_link_libraries(${PROJECT_NAME} GL glfw) endif() 3. 结果 windows 在这里插入图片描述ubuntu 在这里插入图片描述

License

License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎

如需转载请标明出处:https://blog.csdn.net/itas109 技术交流:129518033

Reference: NULL



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有